package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	// "net/url"
	// "encoding/xml"
	"strings"
)

type RstParam struct {
	WsdlUrl    string
	NameSpase  string
	MethodName string
	JsonParam  *FocParam
}

type FocParam struct {
	FltNum   string
	FltDate  string
	OrigCode string
	DestCode string
	VipType  string
}

func getSoapXml(wsdlPath string) {
	strings.HasSuffix(wsdlPath, "?wsdl")
	wsdlPath += "?wsdl"
	request, err := http.Get(wsdlPath)
	defer request.Body.Close()
	checkError(err)
	body, err := ioutil.ReadAll(request.Body)
	checkError(err)
	fmt.Println(string(body))

}
func checkError(err error) {
	if err != nil {
		panic(err)
	}
}
func http2WebService(r *RstParam) (string, error) {
	// fmt.Printf("methodName = %s , nameSpase = %s, methodName = %s\n", r.WsdlUrl, r.NameSpase, r.MethodName)
	js, err := json.Marshal(r.JsonParam)
	if err != nil {
		return "", err
	}
	getSoapXml(r.WsdlUrl)
	soapData := `<?xml version = "1.0" ?>
	<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.sasis.com">
	<soapenv:Header/><soapenv:Body>
	<web:queryPsgForFOC soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
	<in0 xsi:type="web:queryPsgForFOC">
	{"fltNum":"ZH9112","fltDate":"2017-06-20","origCode":"SZX","vipType":"F\\/300","destCode":"HGH"}
	</in0>
	</web:queryPsgForFOC>
	</soapenv:Body>
	</soapenv:Envelope>`

	/*var sl []string

	sl = append(sl, "<?xml version = \"1.0\" ?>")
	sl = append(sl, "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ")
	sl = append(sl, "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ")
	sl = append(sl, "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ")
	sl = append(sl, "xmlns:web=\"")
	sl = append(sl, r.NameSpase)
	sl = append(sl, "\">")
	sl = append(sl, "<soapenv:Header/>")
	sl = append(sl, "<soapenv:Body>")
	sl = append(sl, "<web:")
	sl = append(sl, r.MethodName)
	sl = append(sl, " soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">")
	sl = append(sl, "<in0 xsi:type=\"web:")
	sl = append(sl, r.MethodName)
	sl = append(sl, "\">")
	sl = append(sl, strings(js))
	sl = append(sl, "</in0>")
	sl = append(sl, "</web:")
	sl = append(sl, r.MethodName)
	sl = append(sl, ">")
	sl = append(sl, "</soapenv:Body>")
	sl = append(sl, "</soapenv:Envelope>")
	fmt.Println(strings.Join(sl, ""))
	*/
	client := &http.Client{}
	// var clusterinfo = url.Values{}
	// //var clusterinfo = map[string]string{}
	// clusterinfo.Add("name", "Haha")
	// data := clusterinfo.Encode()
	reqest, _ := http.NewRequest("POST", r.WsdlUrl, strings.NewReader(soapData))
	reqest.Header.Set("Content-Type", "text/xml;charset=UTF-8")
	// reqest.Header.Set("Cookie", "name=anny")
	response, _ := client.Do(reqest)
	defer response.Body.Close()
	fmt.Println("response StatusCode is ", response.StatusCode)
	if response.StatusCode == 200 {
		body, _ := ioutil.ReadAll(response.Body)
		// fmt.Printf("response = %s\n", string(body))
		return string(body), nil
	}
	fmt.Printf("jsonParam = %s\n", string(js))
	// fmt.Printf("jsonParam = %s\n", r.JsonParam)
	return "", nil
}

func main() {
	rst := &RstParam{
		"http://192.168.1.115:8080/sasis/services/StatsPsgForFOCService",
		"http://webservice.sasis.com",
		"queryPsgForFOC",
		&FocParam{"ZH0201", "2017-02-01", "SZX", "HGH", ""}}
	data, err := http2WebService(rst)
	if err != nil {
		fmt.Printf("error : %s\n", err.Error())
	}
	fmt.Printf("reponse date = %s\n", data)
}
